package ms.jasim.model.editor.views;

import java.util.List;

import ms.jasim.framework.SolutionSimulator;
import ms.jasim.framework.EventSimulator.SimulatedTreeNode;
import ms.jasim.framework.SolutionSimulator.HistoryEntry;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;

import swing2swt.layout.BorderLayout;

public class SimulationTimelineView extends ViewPart implements ISelectionListener {

	public static final String ID = "ms.jasim.model.editor.views.SimulationTimelineView"; //$NON-NLS-1$
	private SimulationTimelinePanel simulationTimeline;
	private Label label;

	/**
	 * Create contents of the view part
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new BorderLayout(0, 0));

		Composite composite = new Composite(container, SWT.BORDER);
		composite.setLayoutData(BorderLayout.NORTH);
		composite.setLayout(new BorderLayout(0, 0));
			
		label = new Label(composite, SWT.None);
		label.setText("");
		label.setLayoutData(BorderLayout.CENTER);
		
		simulationTimeline = new SimulationTimelinePanel(container);
		//
		ISelectionService service = (ISelectionService) getSite().getService(ISelectionService.class);
		service.addSelectionListener(this);
		createActions();
		initializeToolBar();
		initializeMenu();
	}

	@Override
	public void dispose() {
		ISelectionService service = (ISelectionService) getSite().getService(ISelectionService.class);
		service.removeSelectionListener(this);
		super.dispose();
	}
	
	/**
	 * Create the actions
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Initialize the toolbar
	 */
	private void initializeToolBar() {
		IToolBarManager toolbarManager = getViewSite().getActionBars()
				.getToolBarManager();
	}

	/**
	 * Initialize the menu
	 */
	private void initializeMenu() {
		IMenuManager menuManager = getViewSite().getActionBars()
				.getMenuManager();
	}

	@Override
	public void setFocus() {
		// Set the focus
	}

	public void setInput(SolutionSimulator sim, List<HistoryEntry> entries) {
		if (sim != null) {
			label.setText(sim.getSolution().getName());
			simulationTimeline.setSimulator(sim, entries);
		}
		else
			label.setText("");
		
	}


	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		if (selection instanceof StructuredSelection) {
			StructuredSelection sel = (StructuredSelection) selection;
			Object selectItem = sel.getFirstElement();
			if (selectItem instanceof SimulatedTreeNode)
				setInput(((SimulatedTreeNode) selectItem).getSimulator(), ((SimulatedTreeNode) selectItem).getSimulationHistory());
			else if (selectItem instanceof SolutionSimulator)
				setInput((SolutionSimulator) selectItem, null);
		}
	}
}
